//
//  SettingViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//
/*
 设置与帮助界面:点击按钮,跳转界面
 1.设置tableView,代理,数据源方法,自定义plist,不完美->主节点数组,无水平的加载plist.
 2.不显示的cell去掉分隔线
 3.点击意见反馈跳转界面:(1)自定义UIView->内容:反馈信息,提交反馈,联系我们.在跳转界面的时候通过模型中的数据改动相应界面的头标题,导航栏左侧为自定义按钮的样式(2)uitableView->客服电话
 */

#pragma mark -- 我就练练提交代码.

#import "SettingViewController.h"
#import "setupModel.h"
#import "FeedBackController.h"
#import <SVProgressHUD.h>
#import "SVProgressHUD.h"

#pragma mark -- 遵循协议

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak,nonatomic)UITableView *setupTableView;

@property (strong,nonatomic)NSArray *dataArray;

@end

@implementation SettingViewController

#pragma mark -- 懒加载

- (NSArray *)dataArray{

    if(!_dataArray){
    
        _dataArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"setupHelp" ofType:@"plist"]];
        
        NSMutableArray *marray = [NSMutableArray array];
        
        [_dataArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [marray addObject:[setupModel setupModelWithDict:obj]];
            
        }];
        
        _dataArray = marray;
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    UITableView *setupView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    self.setupTableView = setupView;
    
#pragma mark -- 设置代理对象
    
    self.setupTableView.delegate = self;
    
    self.setupTableView.dataSource = self;
    
#pragma mark -- 设置行高
    
    self.setupTableView.rowHeight = 50;
    
    [self.view addSubview:setupView];

#pragma mark -- 去掉多余的分割线.
    
    self.setupTableView.tableFooterView = [UIView new];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

#pragma mark -- 设置cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifer = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    if(!cell){
    
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
    }
    
    setupModel *model = self.dataArray[indexPath.row];

//每个cell对应的一个label,plist文件:数组->N个字典->相应的字符信息
    
    cell.textLabel.text = model.infor;
    
//cell右侧的样式
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

/*
                            ----------以下是点击cell的界面跳转----------
 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

#pragma mark -- 意见反馈
    
    if(indexPath.row == 0){
    
        FeedBackController *feedBack = [[FeedBackController alloc]init];
        
        [self.navigationController pushViewController:feedBack animated:YES];
        
        setupModel *model = self.dataArray[indexPath.row];
        
        feedBack.navigationItem.title = model.infor;
    }
#pragma mark -- 注销用户
    
    else if (indexPath.row == 3){
    
        UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"你确定要注销吗?" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
           
#pragma mark -- 点击确定的时候-->出现弹窗提示注销成功->第三方框架SVP
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"b" object:nil];
            
            [SVProgressHUD showSuccessWithStatus:@"注销成功"];
            
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
            
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeCustom];

//C语言系统内延迟调用的方法.将第三方框架释放掉
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
               
                [SVProgressHUD dismiss];
                
            });
        }];
        
        [alterController addAction:sureAction];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alterController addAction:cancelAction];
        
        [self presentViewController:alterController animated:YES completion:nil];
    }
    
}

@end
